﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Petaframe.DataAccess.HandmadeIntegrationTests
{
    public partial class Form1 : Form
    {
        IDataService sqlserver;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sqlserver = new SqlServerDataService("data source=.;initial catalog=northwind;integrated security=true");
        }

        private void btnFindBy_Click(object sender, EventArgs e)
        {
           // var result=  sqlserver.FindBy<Category>();
            sqlserver.AddInputParameter("@p1", 2, DbType.Int32);
            var result = sqlserver.FindBy<Category>(" WhERe categoryID=@p1");          

            toolStripStatusLabel1.Text = string.Format("{0}-{1}-{2}", result.CategoryID, result.CategoryName, result.Description);
        }

        private void btnFindAll_Click(object sender, EventArgs e)
        {
            var result = sqlserver.FindAll<Category>();
            //sqlserver.AddInputParameter("@p1", 7, DbType.Int32);
            //var result = sqlserver.FindAll<Category>("select * from categories where categoryID>@p1"); 

            dataGridView1.DataSource = result;
        }

        private void btnInsertItem_Click(object sender, EventArgs e)
        {
            Category c = new Category() {CategoryName="kaşık",Description="kaşıklamaya yarar" };

            var result = sqlserver.Insert(c);
            //sqlserver.AddInputParameter("@p1", "elma", DbType.String);
            //sqlserver.AddInputParameter("@p2", "elma suyu", DbType.String);
            //var result = sqlserver.Insert("insert into categories(CategoryName,Description) values(@p1,@p2)",CommandType.Text);

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            sqlserver.AddInputParameter("@p1",30 , DbType.Int32);
            var result = sqlserver.FindBy<Category>(" where categoryID=@p1");

            result.CategoryName = "deneme";
            object result2=sqlserver.Update(result);
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            sqlserver.AddInputParameter("@p1", 30, DbType.Int32);
            var result = sqlserver.FindBy<Category>(" where categoryID=@p1");
            var result2 = sqlserver.Delete(result);
            sqlserver.Delete("");
        }

        int page = 1;
        private void btnPaging_Click(object sender, EventArgs e)
        {
            int size=10;
           //dataGridView1.DataSource= sqlserver.Page<Products>(page, size).items;
            PagedResults<Products> result = sqlserver.Page<Products>(page, size, "where unitprice>20");
            dataGridView1.DataSource = result.items;
            toolStripStatusLabel1.Text = String.Format("Page :{0} - total page={1} - total records :{2}", page, result.TotalPages, result.TotalRecords);
            page++;

           page++;
        }

        DataTable dt;
        private void btnDetachedFindAll_Click(object sender, EventArgs e)
        {
            dt=sqlserver.DetachedQuery.FindAll("select * from products");
           // sqlserver.AddInputParameter("@p1", 20, DbType.Decimal);

           //dt= sqlserver.DetachedQuery.FindAll("select c.CategoryName,p.ProductName,p.UnitPrice from categories c join products p on c.categoryID=p.CategoryID where p.UnitPrice>@p1 order by p.UnitPrice");
          

           dataGridView1.DataSource = dt;
        }

        private void btnPersistTable_Click(object sender, EventArgs e)
        {            
            dt.TableName = "Products";
           // var result=  sqlserver.DetachedQuery.PersistTable(dt);

            sqlserver.BeginTransaction();
            var result = sqlserver.DetachedQuery.PersistTable(dt);
            sqlserver.CommitTransaction();

        }

        int index = 1;
        private void btnDetachedPaging_Click(object sender, EventArgs e)
        {
             sqlserver.AddInputParameter("@p1", 20, DbType.Decimal);

             dt = sqlserver.DetachedQuery.Page("select p.ProductID, c.categoryname,p.productname,p.unitprice from categories c join products p on c.categoryId=p.categoryId where p.unitprice>@p1 order by p.ProductID", index, 7);

            dataGridView1.DataSource=dt;
            index++;

        }

        private void btnUnitOfWork_Click(object sender, EventArgs e)
        {
            UnitOfWork uow = new UnitOfWork(sqlserver);
            Category c=new Category {CategoryID=34};

            uow.Delete(c);

            uow.Delete("delete from categories where categoryID=@p1");
            uow.AddInputParameter("@p1", 35, DbType.Int32);

            Category c2 = new Category {CategoryName="test100",Description="dasdad" };
            uow.Insert(c2);
            uow.Insert("insert into categories(categoryname,description) values(@p1,@p2)");
            uow.AddInputParameter("@p1", "test301", DbType.String);
            uow.AddInputParameter("@p2", "testetst", DbType.String);

            c2.CategoryName = "yeni kategori";
            uow.Update(c2);
            //uow.Update("update categories set categoryname=@p1 where categroyname=@p2");
            uow.Commit();
            
        }

        private void btnExpression_Click(object sender, EventArgs e)
        {
             //Category c=new Category {CategoryID=34};
             //Products p = new Products { ProductID=23};

             //Stopwatch sw = new Stopwatch();
             //sw.Start();

             //for (int i = 0; i < 30000; i++)
             //{
             //    //if(i % 2 ==0)
             //       (sqlserver as DataService).ExpressionTest(typeof(Category));
             //    //else
             //    //    (sqlserver as DataService).ExpressionTest<Products>(p);
             //}

             //sw.Stop();
             //label1.Text = sw.Elapsed.TotalSeconds.ToString();

             //sw.Reset();

             //sw.Start();

             //for (int i = 0; i < 30000; i++)
             //{
             //    //if (i % 2 == 0)
             //    (sqlserver as DataService).ReflectionTest(typeof(Category));
             //    //else
             //    //    (sqlserver as DataService).ReflectionTest<Products>(p);
                 
             //}

             //sw.Stop();
             //label2.Text = sw.Elapsed.TotalSeconds.ToString();
        }
    }
}
